#include "led_display.h"
#include "mcu_api.h"
#include "wifi.h"
#include "config.h"
#include "music.h"
#include "sub_device.h"


typedef struct
{
   uint8_t  mode;
   uint16_t timer_cnt;
   uint8_t  flag;
   uint8_t  cycle_cnt;
   
}led_display_struct;

led_display_struct blue_display;
led_display_struct red_display;

volatile uint8_t led_int_avoid;

void set_blue_display_mode(uint8_t mode)
{
   blue_display.timer_cnt=0;
   blue_display.flag=0;
   blue_display.mode =mode;
   blue_display.cycle_cnt=5;
   if(mode!=BLUE_LED_IDLE)
   BLUE_LED_ON;
   else
   BLUE_LED_OFF;
}

void set_red_display_mode(uint8_t mode)
{
   red_display.timer_cnt=0;
   red_display.flag=0;
   red_display.mode =mode;
   red_display.cycle_cnt=5;
   if(mode!=RED_LED_IDLE)
   RED_LED_ON;
   else
   RED_LED_OFF;
}

uint8_t close_red_display_addsub_mode(void)
{
   if(red_display.mode==ADD_SUBDEV_MODE)
   {
       set_red_display_mode(RED_LED_IDLE);
       return 1;
   }
   return 0;
}

uint8_t close_gateway_pair_mode(void)
{
   if(blue_display.mode==GATEWAY_PAIR_MODE)
   {
       set_blue_display_mode(BLUE_LED_IDLE);
       return 1;
   }
   return 0;
}

void factory_all_led(void)
{
 led_int_avoid = 1;
          BLUE_LED_ON;
          RED_LED_ON;
          delay_1ms(100);
          BLUE_LED_OFF;
          RED_LED_OFF;
          delay_1ms(100);
          BLUE_LED_ON;
          RED_LED_ON;
          delay_1ms(100);
          BLUE_LED_OFF;
          RED_LED_OFF;
          
          led_int_avoid = 0;

}

void led_timer_int_handler(void)
{
    static uint8_t cnt;
    if(led_int_avoid==1)
    return;
    blue_display.timer_cnt++;
    red_display.timer_cnt++;
    switch(blue_display.mode)
    {
       case VOL_CHANGE_MODE:
       case MUSIC_CHANGE_MODE:
       case DOORBELL_PLAY_MODE:
            if(blue_display.timer_cnt>=20)
            {
               blue_display.timer_cnt=0;
               if(blue_display.flag)
               {
                  BLUE_LED_ON;
               }
               else
               {
                  BLUE_LED_OFF;
               }
               blue_display.flag=!blue_display.flag;
               if(MUSIC_BUSY_LEVEL_GET())
               {
                  set_blue_display_mode(BLUE_LED_IDLE);
                  //gd_printf(("music playing is finished\r\n"));
               }
            }
       break;
       case GATEWAY_PAIR_MODE:
            if(blue_display.timer_cnt>=100)
            {
               blue_display.timer_cnt=0;
               if(blue_display.flag)
               {
                  BLUE_LED_ON;
               }
               else
               {
                  BLUE_LED_OFF;
               }
               blue_display.flag=!blue_display.flag;
            }
       break;
       case GATEWAY_UNPAIR_MODE:
            if(blue_display.timer_cnt>=20)
            {
               blue_display.timer_cnt=0;
               if(blue_display.flag)
               {
                  BLUE_LED_ON;
               }
               else
               {
                  BLUE_LED_OFF;
               }
               blue_display.flag=!blue_display.flag;
               /*blue_display.cycle_cnt--;
               if(blue_display.cycle_cnt==0)
               {
                  blue_display.mode=BLUE_LED_IDLE;
                  BLUE_LED_OFF;
               }*/
               if(MUSIC_BUSY_LEVEL_GET())
               {
                  set_blue_display_mode(BLUE_LED_IDLE);
                  //gd_printf(("music playing is finished\r\n"));
               }
            }
       break;
       case BLUE_LED_IDLE:

       break;
    }

    switch(red_display.mode)
    {
       case ADD_SUBDEV_MODE:
            
            if(red_display.timer_cnt>=50)
            {
               red_display.timer_cnt=0;
               if(red_display.flag)
               {
                  RED_LED_ON;
               }
               else
               {
                  RED_LED_OFF;
               }
               red_display.flag=!red_display.flag;
            }
       break;
       case PRE_GATEWAY_RESET_FACTORY_MODE:
            /*if(red_display.timer_cnt>=cnt)
            {
               red_display.timer_cnt=0;
               if(red_display.flag)
               {
                  RED_LED_ON;
                  cnt = 100;
               }
               else
               {
                  RED_LED_OFF;
                  cnt = 20;
               }
               red_display.flag=!red_display.flag;
            }*/
            RED_LED_ON;
       break;
       case SMARTCONFIG_MODE:
       case PROMPT_SMARTCONFIG_MODE:
            
            if((mcu_get_wifi_work_state()==AP_STATE)&&(red_display.mode==SMARTCONFIG_MODE))
            {
               set_red_display_mode(AP_MODE);
            }
            if(red_display.timer_cnt>=10)
            {
               red_display.timer_cnt=0;
               if(red_display.flag)
               {
                  RED_LED_ON;
               }
               else
               {
                  RED_LED_OFF;
               }
               red_display.flag=!red_display.flag;
            }
       break;
       case AP_MODE:
       case PROMPT_AP_MODE:
            if((mcu_get_wifi_work_state()==SMART_CONFIG_STATE)&&(red_display.mode==AP_MODE))
            {
               set_red_display_mode(SMARTCONFIG_MODE);
            }
            if(red_display.timer_cnt>=200)
            {
               red_display.timer_cnt=0;
               if(red_display.flag)
               {
                  RED_LED_ON;
               }
               else
               {
                  RED_LED_OFF;
               }
               red_display.flag=!red_display.flag;
              
            }
       break;
      
       case GATEWAY_ONLINE_MODE:
            RED_LED_OFF;
       break;
       case GATEWAY_CONFIGED_BUT_NOT_CONNECT_CLOUD:
            RED_LED_ON;
       break;
       case GATEWAY_RESET_FACTORY_MODE:
            RED_LED_ON;
       break;
       case RED_LED_IDLE:

       break;
    }
    
}

void led_display_mainloop(void)
{
   if((red_display.mode==RED_LED_IDLE)&&(mcu_get_wifi_work_state()==WIFI_CONN_CLOUD))
   {
      red_display.mode = GATEWAY_ONLINE_MODE;
   }
   else if((red_display.mode==RED_LED_IDLE)&&(mcu_get_wifi_work_state()==SMART_CONFIG_STATE))
   {
      red_display.mode = SMARTCONFIG_MODE;
   }
   else if((red_display.mode==RED_LED_IDLE)&&(mcu_get_wifi_work_state()==AP_STATE))
   {
      red_display.mode = AP_MODE;
   }
   else if((red_display.mode==RED_LED_IDLE)&&((mcu_get_wifi_work_state()==WIFI_NOT_CONNECTED)||(mcu_get_wifi_work_state()==WIFI_CONNECTED)||\
   (mcu_get_wifi_work_state()==WIFI_LOW_POWER)))
   {
      red_display.mode = GATEWAY_CONFIGED_BUT_NOT_CONNECT_CLOUD;
   }

   
}

void led_dispaly_init(void)
{
   rcu_periph_clock_enable(RCU_GPIOA);
   rcu_periph_clock_enable(RCU_GPIOB);
   gpio_mode_set(RED_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RED_LED_PIN);
   gpio_output_options_set(RED_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RED_LED_PIN);

   gpio_mode_set(BLUE_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, BLUE_LED_PIN);
   gpio_output_options_set(BLUE_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, BLUE_LED_PIN); 
   set_blue_display_mode(BLUE_LED_IDLE);
   set_red_display_mode(RED_LED_IDLE);
   RED_LED_ON;
   BLUE_LED_OFF;
}
